<?php
abstract class ListController extends  Controller{
    protected $error = array();

    protected function heading_title() {
        return "";
    }
    protected function primary_key_name() {
        return "";
    }
    protected function class_path() {
        return "";
    }
    protected function model_name() {
        return "";
    }

    protected function extra_params(){
        return "";
    }

    public function index() {


        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        $this->getList();
    }



    public function add() {

        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $id = $this->{$this->model_name()}->add($this->request->post,$this->request->get);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $extra = $this->extra_params();
            if(utf8_strlen($extra)>0)
                $extra = '&' . $extra;
            $this->response->redirect($this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . $extra . $url, 'SSL'));



        }

        $this->getForm();
    }

    public function edit() {

        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->{$this->model_name()}->edit($this->request->get[$this->primary_key_name()], $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }
            $extra = $this->extra_params();
            if(utf8_strlen($extra)>0)
                $extra = '&' . $extra;
            $this->response->redirect($this->url->link($this->class_path(), 'token=' . $this->session->data['token'] .$extra . $url, 'SSL'));
        }

        $this->getForm();
    }

    public function delete() {

        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        if (isset($this->request->post['selected']) && $this->validateDelete()) {
            foreach ($this->request->post['selected'] as $id) {
                $this->{$this->model_name()}->delete($id);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }
            $extra = $this->extra_params();
            if(utf8_strlen($extra)>0)
                $extra = '&' . $extra;
            $this->response->redirect($this->url->link($this->class_path(), 'token=' . $this->session->data['token'] .$extra . $url, 'SSL'));
        }

        $this->getList();
    }

    protected function getList() {
        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = $this->primary_key_name();
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'DESC';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->heading_title(),
            'href' => $this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . $url, 'SSL')
        );

        $extra = $this->extra_params();
        if(utf8_strlen($extra)>0)
            $extra = '&' . $extra;

        $data['add'] = $this->url->link($this->class_path().'/add', 'token=' . $this->session->data['token'] .$extra  . $url, 'SSL');
        $data['delete'] = $this->url->link($this->class_path().'/delete', 'token=' . $this->session->data['token'] .$extra . $url, 'SSL');

        $filter_data = array(
            'sort'  => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
            'limit' => $this->config->get('config_limit_admin')
        );

        $ret = array();

        $total_count = $this->get_list_data($ret,$filter_data,$url);
        foreach ($ret as &$result) {
            $result['edit'] = $this->url->link($this->class_path().'/edit', 'token=' . $this->session->data['token']  .$extra . '&'.$this->primary_key_name(). '=' . $result[$this->primary_key_name()] . $url, 'SSL');
        }
        $data['data'] =$ret;

        $this->push_data($data);

        $data['token'] = $this->session->data['token'];

        $data['primary_key_name'] = $this->primary_key_name();

        $data['heading_title'] = $this->heading_title();

        $data['text_list'] = $this->heading_title();
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');



        $data['button_add'] = $this->language->get('button_add');
        $data['button_edit'] = $this->language->get('button_edit');
        $data['button_delete'] = $this->language->get('button_delete');

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        if (isset($this->request->post['selected'])) {
            $data['selected'] = (array)$this->request->post['selected'];
        } else {
            $data['selected'] = array();
        }

        $url = '';

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }


        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $pagination = new Pagination();
        $pagination->total = $total_count;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_limit_admin');
        $pagination->url = $this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . $extra . $url . '&page={page}', 'SSL');

        $data['pagination'] = $pagination->render();

        $data['results'] = sprintf($this->language->get('text_pagination'), ($total_count) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($total_count - $this->config->get('config_limit_admin'))) ? $total_count : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $total_count, ceil($total_count / $this->config->get('config_limit_admin')));

        $data['sort'] = $sort;
        $data['order'] = $order;

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view($this->class_path().'_list.php', $data));
    }

    protected function get_list_data(&$ret,$filter_data,$url){

        return 0;
    }

    protected function push_data(&$data){

    }

    protected function getForm() {
        $data['heading_title'] = $this->heading_title();

        $data['text_form'] = !isset($this->request->get[$this->primary_key_name()]) ? '添加' : '编辑';
        $data['text_yes'] = $this->language->get('text_yes');
        $data['text_no'] = $this->language->get('text_no');

        $data['entry_name'] = $this->language->get('entry_name');
        $data['entry_description'] = $this->language->get('entry_description');
        $data['entry_approval'] = $this->language->get('entry_approval');
        $data['entry_sort_order'] = $this->language->get('entry_sort_order');

        $data['help_approval'] = $this->language->get('help_approval');

        $data['button_save'] = $this->language->get('button_save');
        $data['button_cancel'] = $this->language->get('button_cancel');

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->error['name'])) {
            $data['error_name'] = $this->error['name'];
        } else {
            $data['error_name'] = array();
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->heading_title(),
            'href' => $this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . $url, 'SSL')
        );

        $extra = $this->extra_params();
        if(utf8_strlen($extra)>0)
            $extra = '&' . $extra;

        if (!isset($this->request->get[$this->primary_key_name()])) {
            $data['action'] = $this->url->link($this->class_path().'/add', 'token=' . $this->session->data['token'] . $extra . $url, 'SSL');
            $data['primary_key'] = 0;
        } else {
            $data['action'] = $this->url->link($this->class_path().'/edit', 'token=' . $this->session->data['token'] . $extra . '&' . $this->primary_key_name() . '=' . $this->request->get[$this->primary_key_name()] . $url, 'SSL');
            $data['primary_key'] = $this->request->get[$this->primary_key_name()];
        }

        $data['cancel'] = $this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . $extra . $url, 'SSL');



        $this->get_form_data($data);

        $data['token'] = $this->session->data['token'];

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view($this->class_path().'_form.php', $data));
    }

    protected function get_form_data(&$data){

    }

    protected function validateForm() {
        return true;
    }



    protected function validateDelete() {
        return true;
    }
}